/* 
 *  <copyright file="PhysicsBodyCmp.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using SobrietyEngine.Physics;



namespace SobrietyEngine.Components.Physics
{
    public class FarseerPhysicsCmp : BaseComponent, IFarseerPhysicsBody, IComponent
    {
        public Body Body { get; set; }

        protected FarseerPhysicsService physicsGameService;
        protected String physicsWorldName = "";
        protected World world;

        protected IPosition position;
        protected IOrientation rotation;

        bool convertUnits = false;

        private float mass = 5.0f;

        public float Mass
        {
            get { return mass; }
            set { }
        }
        private Boolean ignoreGravity = false;

        public Boolean IgnoreGravity
        {
            get { return ignoreGravity; }
            set { ignoreGravity = value; Body.IgnoreGravity = ignoreGravity; }
        }

        protected float linearDragCoefficient;

        private Boolean isStatic = false;//none moving?

        public Boolean IsStatic
        {
            get { return isStatic; }
            set { isStatic = value; if (Body != null) { Body.IsStatic = isStatic; } }
        }
        protected Boolean ControlRotation = true;//Physics controls our Orientation

        protected Boolean enabled = true;
        public Boolean Enabled { get { return enabled; } 
            set { 
                enabled = value;
                if (Body!=null)
                        Body.Enabled = enabled;
            } 
        }

        public float MomentOfInertia { get; set; }

        public FarseerPhysicsCmp()
            : base()
        {
        }

        public FarseerPhysicsCmp(String _worldName)
            : base()
        {
            physicsWorldName = _worldName;
        }

        override public Type[] SupportedInterfaces()
        {
            return new Type[] { typeof(IFarseerPhysicsBody) };
        }

        override public Boolean InitComponent()
        {
            base.InitComponent();

            position = Actor.Get<IPosition>();
            rotation = Actor.Get<IOrientation>();

            Body.UserData = Actor;

            if (!IsStatic)
                Body.BodyType = BodyType.Dynamic;
            else
            {              
                //if we are static, we dont need to update
                Body.BodyType = BodyType.Static;
               //set the positions to be th ephysics position
                position.PositionV2 = Body.Position;
                //turn off this component
                Active = false; 
            }

            if (convertUnits)
                Body.Position = physicsGameService.ConvertUnits.ToSimUnits(position.PositionV2);
            else
                Body.Position = position.PositionV2;

            Body.IgnoreGravity = IgnoreGravity;
            Body.SleepingAllowed = true;

            return true;
        }

        public new void Update(GameTime elapsedTime)
        {
            if (IsStatic)
            {
                if (convertUnits)
                    Body.Position = physicsGameService.ConvertUnits.ToSimUnits(position.PositionV2);
                else
                    Body.Position = position.PositionV2;
            }
            else
            {
                if (convertUnits)
                    position.PositionV2 = physicsGameService.ConvertUnits.ToDisplayUnits(Body.Position);
                else
                    position.PositionV2 = Body.Position;
            }

            if (ControlRotation &&! IsStatic && rotation != null)
                rotation.Set(Body.Rotation);

            else if (IsStatic)
            {
                if(rotation!=null)                    
                    Body.Rotation = rotation.GetRadiansXY();
            }
        }
       
        
        public new void Remove()
        {
            world.RemoveBody(Body);
        }
    }
}
